<?php
namespace app\common\model;

use Lvht\GeoHash;
use think\Db;

class UserSiteModel extends BaseModel
{

    protected $name = 'user_site'; //定义用户表
    //开启自动写入时间戳
    protected $autoWriteTimestamp = false;
    // 定义自动完成的属性
    protected $auto = []; //新增修改
    protected $insert = []; //插入
    protected $update = []; //修改
    //类型自动转换
    protected $type = [];

    //添加店铺信息
    public function addData($data)
    {
        if (!empty($data['coordinate'])){
            $area=explode(',',$data['coordinate']);
            $data['lng']=$area[0];
            $data['lat']=$area[1];
            $data['geohash']=GeoHash::encode($area[0],$area[1]);
        }
        if ($data['dist_id'] > 0) {
            $data['area_name'] = Db::table('area')->where('id', $data['dist_id'])->value('path');
            return $this->isUpdate(false)->allowField(true)->save($data);
        }elseif($data['city_id'] > 0){
            $data['area_name'] = Db::table('area')->where('id', $data['city_id'])->value('path');
            return $this->isUpdate(false)->allowField(true)->save($data);
        }else{
            $data['area_name'] = Db::table('area')->where('id', $data['prov_id'])->value('path');
            return $this->isUpdate(false)->allowField(true)->save($data);
        }
        return $this->isUpdate(false)->allowField(true)->save($data);
    }

    //修改店铺信息
    public function editData($data)
    {
        if (!empty($data['coordinate'])){
            $area=explode(',',$data['coordinate']);
            $data['lng']=$area[0];
            $data['lat']=$area[1];
            $data['geohash']=GeoHash::encode($area[0],$area[1]);
        }
        if ($data['dist_id'] > 0) {
            $data['area_name'] = Db::table('area')->where('id', $data['dist_id'])->value('path');
            return $this->isUpdate(false)->allowField(true)->save($data);
        }elseif($data['city_id'] > 0){
            $data['area_name'] = Db::table('area')->where('id', $data['city_id'])->value('path');
            return $this->isUpdate(false)->allowField(true)->save($data);
        }else{
            $data['area_name'] = Db::table('area')->where('id', $data['prov_id'])->value('path');
            return $this->isUpdate(false)->allowField(true)->save($data);
        }
        return $this->isUpdate(true)->allowField(true)->save($data);
    }

    /**
     * 站点对应上游模型 关联站点模型
     * @param $siteId
     */
    public function upSiteModel($siteId){
        //1、通过站点ID取到对应会员ID组
        $userModel = new UserModel();
        $username = $userModel->where('site_id',$siteId)->value('username');
        //2、通过username查找该站点注册得会员
        $siteIds = $userModel->where('username',$username)->where('utype','member')->column('site_id');
        $siteIds = empty($siteIds)?[0]:$siteIds;
        //3、返回站点模型
        $model = $userModel->getListModel('site')->where('utype','site')->where('site_id','in',$siteIds);
        return $model;
    }

    /**
     * 站点对应下游模型 关联会员模型
     * @param $siteId
     */
    public function downSiteModel($siteId){
        $userModel = new UserModel();
        $model = $userModel->getListModel('member')->where('utype','member')->where('site_id',$siteId);
        return $model;
    }

    /*读取器*/

    /*修改器*/

}